package com.isoft.service.impl;

import com.isoft.entity.BaseCfueEntity;
import com.isoft.service.BaseCfueService;
import com.isoft.service.XmlFileHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;

@Component
public class BaseCfueFileHandler implements XmlFileHandler {
    private static final Logger logger = LoggerFactory.getLogger(BaseCfueFileHandler.class);

    @Autowired
    private BaseCfueService baseCfueService;

    @Override
    public boolean supports(Path filePath) {
        try {
            String fileName = filePath.getFileName().toString().toUpperCase();
            // 支持文件名包含BASE-CFUE
            return fileName.contains("BASE-CFUE");
        } catch (Exception e) {
            logger.warn("Error checking file support for {}: {}", filePath, e.getMessage());
            return false;
        }
    }

    @Override
    public String handle(Path filePath) throws Exception {
        try {
            logger.info("开始处理BASE-CFUE文件: {}", filePath.getFileName());
            
            // 读取文件内容
            String xmlContent = Files.readString(filePath);
            
            // 解析并保存数据
            String result = baseCfueService.parseAndSave(xmlContent);
            
            logger.info("BASE-CFUE文件处理成功: {}, 结果: {}", filePath.getFileName(), result);
            return result;
        } catch (Exception e) {
            logger.error("处理BASE-CFUE文件失败: {}", filePath.getFileName(), e);
            throw new RuntimeException("处理XML失败: " + e.getMessage(), e);
        }
    }
} 